<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class InfoController extends Controller
{
    public function index()
    {
        $ret=User::where('user_id',session('user_id'))->first();
        return view('admin.info',compact('ret'));
    }
    public function update(Request $request)
    {
        $message=[
            'user_name.required'=>'昵称不能为空!',
            'user_phone.required'=>'手机号不能为空!',
            'user_phone.numeric'=>'手机格式不正确!',
            'user_phone.digits_between'=>'手机格式不正确!'
        ];
        $rule=[
            'user_name'=>'required',
            'user_phone'=>'required|numeric|digits_between:11,11'
        ];
        $this->validate($request,$rule,$message);
        $user=User::where('user_id',session('user_id'))->first();
        $user->user_name=$request->user_name;
        $user->user_phone=$request->user_phone;
        if($user->update())
        {
            return redirect('charge/info.html')->with('success_msg','信息修改成功!');
        }else
        {
            return back()->with('error_msg','信息修改失败，请稍后再试!');
        }

    }

    /**
     * 用户修改密码
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function changePassword(Request $request)
    {

        $message=[
          'required'=>'新密码不能为空！',
            'same'=>'两次输入密码不一致！'
        ];
        $rule=[
            'password_n'=>'required|same:password_s'
        ];
        $this->validate($request,$rule,$message);
        $user=User::where('user_id',session('user_id'))->first();
        if($user->user_password===md5($request->password_o))
        {
            $user->user_password=md5($request->password_n);
            $ret=$user->update();
            if($ret)
            {
                return redirect('charge/info.html')->with('success_msg','密码修改成功！');
            }else{
                return back()->with('error_msg','密码修改失败，请稍后重试！');
            }
        }else
        {
            return back()->with('error_msg','原密码不正确，请重试！');
        }

    }
}
